﻿using LightCAD.Core;
using LightCAD.Core.Elements;
using LightCAD.Runtime;
using System;
using System.Collections.Generic;

namespace QdArch
{
  
    public class QdWindow : LcComponent
    {
        public static Shape2dProvider ShapeProvider;
        public static ParameterSetDefinition ParamsDefinition;
        static QdWindow()
        {
            ShapeProvider = Shape2dManager.LoadProvider(ArchCategory.Window.ToString());

            ParamsDefinition = new ParameterSetDefinition() {
                 new ParameterDefinition
                 {
                     Name = "Thickness",
                     DataType= LcDataType.Double,
                 },
                 new ParameterDefinition
                 {
                     Name = "Width",
                      DataType= LcDataType.Double,
                 },
                 new ParameterDefinition
                 {
                     Name = "Height",
                     DataType= LcDataType.Double,
                 },
                 new ParameterDefinition
                 {
                     Name = "BottomOffset",
                     DataType= LcDataType.Double,
                 },
                 new ParameterDefinition
                 {
                     Name = "IsLeft",
                     DataType= LcDataType.Bool,
                 },
                 new ParameterDefinition
                 {
                     Name = "IsNear",
                     DataType= LcDataType.Bool,
                 },
            };

        }
        public QdWindow()
        {
            base.Category = ArchCategory.Window.ToString();
            this.Uuid = Guid.NewGuid().ToString();
        }
        public QdWindow(string subCategory, string name) : this()
        {
            base.SubCategory = subCategory;
            base.Name = name;
            this.Shape2D = ShapeProvider.Shapes.GetShape(name);
        }
    }

    public class QdWindowRef : LcComponentRef, IElement3D
    {

        public LcParameterSet Parameters { get; set; }
        public QdWindow Window { get; set; }
        /// <summary>
        /// 在墙上用中心插入
        /// </summary>
        public QdWall Wall { get; set; }

        public Vector2d StartPoint { get; set; }
        public Vector2d EndPoint { get; set; }
        public Vector2d ControlPoint { get; set; }

        public double Thickness { get; set; }
        public double Width { get; set; }
        public double Height { get; set; }
        public double Bottom { get; set; }

        public QdWindowRef()
        {
            this.Type = ArchElementType.Window;
        }

        public override Box2d GetBoundingBox()
        {
            if (this.Parameters == null || this.Parameters.Values.Length == 0)
            {
                return Box2d.Empty;
            }

            return new Box2d(this.InsertPoint, Width, Height);
        }

        public override bool IntersectWithBox(Polygon2d testPoly, List<RefChildElement> intersectChildren = null)
        {
            Box2d thisBox = this.BoundingBox;
            return thisBox.IntersectWith(testPoly.BoundingBox);
        }

        public override bool IncludedByBox(Polygon2d testPoly, List<RefChildElement> includedChildren = null)
        {
            Box2d thisBox = this.BoundingBox;
            return thisBox.IntersectWith(testPoly.BoundingBox);
        }

        public override LcElement Clone()
        {
            var clone = Document.CreateObject<QdDoorRef>();
            clone.Copy(this);
            clone.Initilize(this.Document);
            return clone;
        }

        public override void Copy(LcElement src)
        {
            base.Copy(src);
            var doorRef = (QdWindowRef)src;
            this.Parameters = doorRef.Parameters;
            this.Window = doorRef.Window;
            this.Wall = doorRef.Wall;
            this.StartPoint = doorRef.StartPoint;
            this.EndPoint = doorRef.EndPoint;
            this.ControlPoint = doorRef.ControlPoint;
            this.Width = doorRef.Width;
            this.Height = doorRef.Height;
            this.Bottom = doorRef.Bottom;

        }
    }
}